package com.pengtu.gsj.service.impl.check.yh.technical;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.RoadRectification;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.check.yh.technical.TechnicalSupervisionService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.service.yh.RoadRectificationService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class TechnicalSupervisionServiceImpl implements TechnicalSupervisionService {
	
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private DownService downService;
	@Autowired
	private RoadRectificationService roadRectificationService;

	@Override
	public Map<String, Object> getCheckContent(String startTime,
			String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<List<String>> totalScoreList = new ArrayList<List<String>>();
		List<Object> values = new ArrayList<Object>();
		List<RoadRectification> roadRectifications = new ArrayList<RoadRectification>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		
		double total =0; //检查得分
		double percentage =0; //系统完善度
		double tableFull = yearFull*4 + yearPart;
		double ministerial = 0; //部级督办里程数
		double provincial = 0; //省级督办里程数
		double rectification = 0; //已整改里程数
		double rectificationRate = 0; //已整改里程数
		double stepScore = 0; //整改率对应的 分数
		//查询所有相关的附件
		List<Attachment> list = systemManageService.getCheckAttachments(startTime, String.valueOf(Integer.parseInt(endTime)-1), tag);
		
		roadRectifications = roadRectificationService.getCheckRoadRectifications(startTime, String.valueOf(Integer.parseInt(endTime)-1));
		for (RoadRectification roadRectification : roadRectifications) {
			double result = 0;
			List<String> resultList = Arrays.asList("0", "0");
			//判断是否有无将计划纳入管理考核
			if (roadRectification.getAttachments().size() > 0 ) {
				result = MathsUtils.sumDouble(result, yearFull);
				resultList.set(0, "1");
				resultList.set(1, String.valueOf(result));
			}
			//将相关的业务文件添加在文件集合中
			for (Attachment attachment : roadRectification.getAttachments()) {
				list.add(attachment);
			}
			total += result;
			//总计整改里程数及计算整改率
			ministerial = MathsUtils.sumDouble(ministerial, roadRectification.getMinisterial());
			provincial = MathsUtils.sumDouble(provincial, roadRectification.getProvincial());
			rectification = MathsUtils.sumDouble(rectification, roadRectification.getRectification());
			
			totalScoreList.add(resultList);
		}
		rectificationRate =  MathsUtils.divDouble(rectification*100, MathsUtils.sumDouble(ministerial, provincial), 2);
		if ((ministerial == 0 && provincial == 0) || rectificationRate >= 90) {
			total = MathsUtils.sumDouble(total, yearPart);
			stepScore = yearPart;
		} else if (rectificationRate >= 50 && rectificationRate < 90) {
			total = MathsUtils.sumDouble(total, poitMark);
			stepScore = poitMark;
		}
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		//需要更新数据
		values.add("true");
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("percentage", checkTable.getPerfection());
		map.put("checkTable", checkTable);
		map.put("roadRectifications", roadRectifications);
		map.put("totalScoreList", totalScoreList);
		map.put("ministerial", ministerial);
		map.put("provincial", provincial);
		map.put("rectification", rectification);
		map.put("rectificationRate", rectificationRate);
		map.put("stepScore", stepScore);
		return map;
	}

}
